import java.util.Stack;

public class Main {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char str = s.charAt(i);
            if (str == '(' || str == '{' || str == '[') {
                stack.push(str);
            } else {
                if (stack.isEmpty())
                    return false;
                char topStr = stack.pop();
                if (str == ')' && topStr != '(')
                    return false;
                if (str == '}' && topStr != '{')
                    return false;
                if (str == ']' && topStr != '[')
                    return false;
            }
        }
        return stack.isEmpty();
    }
}
